package com.cn.qgx428.lazydiary.ui.activity

import android.graphics.Color
import android.os.Bundle
import android.text.TextUtils
import com.cn.qgx428.lazydiary.Presenter.UserPresenter
import com.cn.qgx428.lazydiary.Presenter.contract.UserContract
import com.cn.qgx428.lazydiary.R
import com.cn.qgx428.lazydiary.Utils.SPUtil
import com.cn.qgx428.lazydiary.local.LAMessege
import com.cn.qgx428.lazydiary.local.LAction
import com.cn.qgx428.lazydiary.local.LActionWacher
import com.cn.qgx428.lazydiary.local.LManager
import com.cn.qgx428.lazydiary.network.bean.User
import kotlinx.android.synthetic.main.activity_login.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

class LoginActivity : BaseActivity(), LActionWacher {

    override fun onTrigger(action: String, msg: LAMessege) {
        if (TextUtils.equals(action, LAction.ACTION_REGISTER)){
            login_phone.setText(msg.arg1)
            login_pwd.setText(msg.arg2)
        }
    }

    override fun initUI() {
        setStatuBarColor(Color.BLACK)
        hindActionBar()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)

        LManager.newInstance().register(this,LAction.ACTION_REGISTER)
        initView()
        getData()
    }

    private fun getData() {
        val sp = SPUtil.get("user")
        login_phone.setText(sp.getString("phone",""))
        login_pwd.setText(sp.getString("pwd",""))
    }

    fun initView(){
        login_cancelBtn.setOnClickListener({finish()})
        login_registerBtn.setOnClickListener({startActivity<RegisterActivity>()})
        login_loginBtn.setOnClickListener({
            if (checkInfo()){
                UserPresenter.login(login_phone.text.toString(), login_pwd.text.toString(),
                        object: UserContract.IUserLogin{
                            override fun onLogin(user: User, msg: String) {
                                toast(msg)
                                SPUtil.saveUserInfo(user.phone,user.pwd,user.id.toString(),user.nickname)
                                SPUtil.rememberUserLogin()
                                LManager.newInstance().trigger(LAction.ACTION_LOGIN)
                                finish()
                            }

                            override fun onLoginFailed(msg: String) {
                               toast(msg)
                            }

                        })
            }
        })
    }

    /**
     * 检查登陆信息
     */
    fun checkInfo(): Boolean{
        if(TextUtils.isEmpty(login_phone.text.toString()) ||
                TextUtils.isEmpty(login_pwd.text.toString())){
            toast("账号或密码不能为空")
            return false
        }
        return true
    }

    override fun finish() {
        super.finish()
        //将入场动画取消
        endAnim(R.anim._loginactivity_out)
    }

    override fun onDestroy() {
        super.onDestroy()
        LManager.newInstance().unRegister(this)
    }
}
